// pages/oneRequest/oneRequest.js
const config = require("../../utils/config.js")
const api = require('../../utils/api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrlPre: "https://boansmith.natapp4.cc/images/",
    imgDriverRoom: "name-driverRoom",
    imgTruckTail: "name-truckTail",
    imgIndex0: 1,
    imgIndex1: 1,
    imgIndex2: 1,
    imgIndex3: 1,
    imgIndex4: 1,
    device_name: '',
    worker_phone: '',
    lastId: "",
    xxitems1: [{
        id: 0,
        name: '无问题',
        checked: false
      },
      {
        id: 1,
        name: '未垫钢板或枕木',
        checked: false
      },
      {
        id: 2,
        name: '支撑臂张开不足',
        checked: false
      },
      {
        id: 3,
        name: '超出起吊极限',
        checked: false
      },
      {
        id: 4,
        name: '摄像头有脏污看不清',
        checked: false
      }
    ],
    xxitems2: [{
        id: 0,
        name: '无问题',
        checked: false
      },
      {
        id: 1,
        name: '未垫钢板或枕木',
        checked: false
      },
      {
        id: 2,
        name: '支撑臂张开不足',
        checked: false
      },
      {
        id: 3,
        name: '超出起吊极限',
        checked: false
      },
      {
        id: 4,
        name: '摄像头有脏污看不清',
        checked: false
      }
    ],
    xxitems3: [{
        id: 0,
        name: '无问题',
        checked: false
      },
      {
        id: 1,
        name: '未垫钢板或枕木',
        checked: false
      },
      {
        id: 2,
        name: '支撑臂张开不足',
        checked: false
      },
      {
        id: 3,
        name: '超出起吊极限',
        checked: false
      },
      {
        id: 4,
        name: '摄像头有脏污看不清',
        checked: false
      }
    ],

    xxitems4: [{
        id: 0,
        name: '无问题',
        checked: false
      },
      {
        id: 1,
        name: '未垫钢板或枕木',
        checked: false
      },
      {
        id: 2,
        name: '支撑臂张开不足',
        checked: false
      },
      {
        id: 3,
        name: '超出起吊极限',
        checked: false
      },
      {
        id: 4,
        name: '摄像头有脏污看不清',
        checked: false
      }
    ],

    xxitems5: [{
        id: 0,
        name: '无问题',
        checked: false
      },
      {
        id: 1,
        name: '未垫钢板或枕木',
        checked: false
      },
      {
        id: 2,
        name: '支撑臂张开不足',
        checked: false
      },
      {
        id: 3,
        name: '超出起吊极限',
        checked: false
      },
      {
        id: 4,
        name: '摄像头有脏污看不清',
        checked: false
      }
    ],


    itemChecked1: false,
    itemChecked2: false,
    itemChecked3: false,
    itemChecked4: false,
    itemChecked5: false,

    itemOk1: true,
    itemOk2: true,
    itemOk3: true,
    itemOk4: true,
    itemOk5: true,

    unlockText: '开锁',

    choices1: [],
    choices2: [],
    choices3: [],
    choices4: [],
    choices5: [],
  },


  onRefresh: function () {
    var that = this
    wx.request({
      url: "https://boansmith.natapp4.cc/refresh",
      data: {
        'device_name': that.data.device_name
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        wx.showLoading({
          title: '加载中...',
        })

        setTimeout(function () {
          wx.hideLoading()
          console.log("comp")
          var index = that.data.imgIndex0 + 1
          // that.setData({
          //   imgIndex0: index
          // })

          var inds = [{imgIndex0:index}, {imgIndex1:index}, {imgIndex2:index}, {imgIndex3:index}, {imgIndex4:index}]
          var curImgIndex = 0
          var ter = setInterval(function() {
            inds[curImgIndex].imgIndex0
              that.setData(
                inds[curImgIndex]
              )
              curImgIndex += 1
              if (curImgIndex == 5) {
                clearInterval(ter)
              }
          }, 100)
        }, 4000)
      },
      fail: function (res) {
        // fail
        console.log(res)
        that.failMessage()
      },
      complete: function () {
        // complete
      }
    })
  },

  onContactWorker: function () {
    var that = this
    wx.makePhoneCall({
      phoneNumber: that.data.worker_phone,
    })
  },

  onUnlock: function () {
    var that = this
    var setChoices = new Set()
    for (var i of that.data.choices1) {
      if (i === "0") {
        continue
      }
      setChoices.add('相机1#:' + that.data.xxitems5[i].name)
    }
    for (var i of that.data.choices2) {
      if (i === "0") {
        continue
      }
      setChoices.add('相机2#:' + that.data.xxitems5[i].name)
    }
    for (var i of that.data.choices3) {
      if (i === "0") {
        continue
      }
      setChoices.add('相机3#:' + that.data.xxitems5[i].name)
    }
    for (var i of that.data.choices4) {
      if (i === "0") {
        continue
      }
      setChoices.add('相机4#:' + that.data.xxitems5[i].name)
    }
    for (var i of that.data.choices5) {
      if (i === "0") {
        continue
      }
      setChoices.add('相机5#:' + that.data.xxitems5[i].name)
    }

    var listChoices = Array.from(setChoices)
    console.log('setChoices[0]', listChoices[0], listChoices.length)

    var strProblems = listChoices.join(" ")

    var isGood = false
    // it is unlock
    if (listChoices.length == 0) {
      isGood = true
    }

    setChoices.clear()

    console.log("isGood: ", isGood)
    if (isGood) {
      wx.showModal({
        title: '提示',
        content: '确定开锁吗?',
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
            api.get('unlockDevice', {
              'device_name': that.data.device_name,
              'worker_phone': that.data.worker_phone,
              'problems': "",
              "lastId": that.data.lastId
            }).then(res => {
              var res2 = res
              console.log("onUnlock: res: ", res)
              wx.showLoading({
                title: '正在操作',
              })

              setTimeout(function () {
                wx.hideLoading()
                if (res == "ok") {
                  wx.showToast({
                    title: '操作成功!',
                    icon: 'none',
                    duration: 2000
                  })

                  wx.navigateBack({
                    delta: 1
                  })
                } else {
                  wx.showToast({
                    title: '操作失败!',
                    icon: 'none',
                    duration: 2000
                  })
                }
              }, 3000)
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '确定拒绝吗?',
        success(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
            api.get('unlockDevice', {
              'device_name': that.data.device_name,
              'worker_phone': that.data.worker_phone,
              'problems': strProblems,
              "lastId": that.data.lastId
            }).then(res => {
              console.log("onUnlock: res: ", res)
              wx.showLoading({
                title: '正在操作',
              })

              setTimeout(function () {
                wx.hideLoading()
                if (res == "ok") {
                  console.log("OKOKOK")
                  wx.navigateBack({
                    delta: 1
                  })
                } else {
                  wx.showToast({
                    title: '操作失败!',
                    icon: 'none',
                    duration: 2000
                  })
                }
              }, 3000)
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('optionsss: ', options.data)

    var listTmp = options.data.split(';')
    var deviceName = listTmp[0]
    console.log("device_name", deviceName)
    wx.setStorageSync('curDevice', deviceName)
    var phone = listTmp[1]
    var lastId = listTmp[2]
    this.setData({
      device_name: deviceName,
      worker_phone: phone,
      lastId: lastId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onCheckboxChange1: function (e) {
    var that = this
    var checkedName = e.detail.value
    var len = checkedName.length
    var last = checkedName[len - 1]
    var newCheckedName = []
    if (0 == last) {
      checkedName = ["0"]
    } else {
      for (var i of checkedName) {
        if (i != '0') {
          newCheckedName.push(i)
        }
      }
      checkedName = newCheckedName
    }
    that.data.choices1 = checkedName


    this.setData({
      itemChecked1: (checkedName.length > 0)
    })

    var okOld = that.data.itemOk1
    if (checkedName.indexOf("0") > -1 && !okOld) {
      var oldItemxx1 = that.data.xxitems1
      oldItemxx1[0].checked = true
      for (var i = 1; i < oldItemxx1.length; ++i) {
        oldItemxx1[i].checked = false
      }
      that.setData({
        itemOk1: true,
        xxitems1: oldItemxx1
      })

      that.data.xxitems1 = oldItemxx1

      var tData = that.data
      if (tData.itemOk1 && tData.itemOk2 && tData.itemOk3 && tData.itemOk4 && tData.itemOk5) {
        that.setData({
          unlockText: '开锁'
        })
      }
    } else if (okOld) {
      if (checkedName.indexOf("0") > -1 && checkedName.length == 1) {
        return
      }

      var oldItemxx1 = that.data.xxitems1
      for (var i in checkedName) {
        console.log("xxxxxx")
        var t = checkedName[i]
        oldItemxx1[t - '0'].checked = true
      }
      oldItemxx1[0].checked = false
      that.data.xxitems1 = oldItemxx1
      that.setData({
        xxitems1: oldItemxx1,
        itemOk1: false,
        unlockText: '拒绝'
      })
    }

    console.log("oldItemxx1: ", that.data.xxitems1)
  },

  onCheckboxChange2: function (e) {
    var that = this
    var checkedName = e.detail.value

    var len = checkedName.length
    var last = checkedName[len - 1]
    console.log('last  ', last)
    var newCheckedName = []
    if (0 == last) {
      checkedName = ["0"]
    } else {
      for (var i of checkedName) {
        if (i != '0') {
          newCheckedName.push(i)
        }
      }
      checkedName = newCheckedName
    }
    that.data.choices2 = checkedName

    this.setData({
      itemChecked2: (checkedName.length > 0)
    })

    var okOld = that.data.itemOk2
    if (checkedName.indexOf("0") > -1 && !okOld) {
      var oldItemxx2 = that.data.xxitems2
      oldItemxx2[0].checked = true
      for (var i = 1; i < oldItemxx2.length; ++i) {
        oldItemxx2[i].checked = false
      }
      that.setData({
        itemOk2: true,
        xxitems2: oldItemxx2
      })

      var tData = that.data
      if (tData.itemOk1 && tData.itemOk2 && tData.itemOk3 && tData.itemOk4 && tData.itemOk5) {
        that.setData({
          unlockText: '开锁'
        })
      }
    } else if (okOld) {
      if (checkedName.indexOf("0") > -1 && checkedName.length == 1) {
        return
      }

      var oldItemxx2 = that.data.xxitems2
      for (var i in checkedName) {
        var t = checkedName[i]
        oldItemxx2[t - '0'].checked = true
      }
      oldItemxx2[0].checked = false
      this.setData({
        xxitems2: oldItemxx2,
        itemOk2: false,
        unlockText: '拒绝'
      })
    }
  },

  onCheckboxChange3: function (e) {
    var that = this
    var checkedName = e.detail.value
    console.log("e.detail: ", e)
    console.log(checkedName)

    var len = checkedName.length
    var last = checkedName[len - 1]
    var newCheckedName = []
    if (0 == last) {
      checkedName = ["0"]
    } else {
      for (var i of checkedName) {
        if (i != '0') {
          newCheckedName.push(i)
        }
      }
      checkedName = newCheckedName
    }
    that.data.choices3 = checkedName

    this.setData({
      itemChecked3: (checkedName.length > 0)
    })

    var okOld = that.data.itemOk3
    if (checkedName.indexOf("0") > -1 && !okOld) {
      var oldItemxx3 = that.data.xxitems3
      oldItemxx3[0].checked = true
      for (var i = 1; i < oldItemxx3.length; ++i) {
        oldItemxx3[i].checked = false
      }
      that.setData({
        itemOk3: true,
        xxitems3: oldItemxx3
      })

      var tData = that.data
      if (tData.itemOk1 && tData.itemOk2 && tData.itemOk3 && tData.itemOk4 && tData.itemOk5) {
        that.setData({
          unlockText: '开锁'
        })
      }
    } else if (okOld) {
      if (checkedName.indexOf("0") > -1 && checkedName.length == 1) {
        return
      }

      var oldItemxx3 = that.data.xxitems3
      for (var i in checkedName) {
        var t = checkedName[i]
        oldItemxx3[t - '0'].checked = true
      }
      oldItemxx3[0].checked = false
      this.setData({
        xxitems3: oldItemxx3,
        itemOk3: false,
        unlockText: '拒绝'
      })
    }
  },

  onCheckboxChange4: function (e) {
    var that = this
    var checkedName = e.detail.value
    console.log("e.detail: ", e)
    console.log(checkedName)

    var len = checkedName.length
    var last = checkedName[len - 1]
    var newCheckedName = []
    if (0 == last) {
      checkedName = ["0"]
    } else {
      for (var i of checkedName) {
        if (i != '0') {
          newCheckedName.push(i)
        }
      }
      checkedName = newCheckedName
    }
    that.data.choices4 = checkedName

    this.setData({
      itemChecked4: (checkedName.length > 0)
    })

    var okOld = that.data.itemOk4
    if (checkedName.indexOf("0") > -1 && !okOld) {
      var oldItemxx4 = that.data.xxitems4
      oldItemxx4[0].checked = true
      for (var i = 1; i < oldItemxx4.length; ++i) {
        oldItemxx4[i].checked = false
      }
      that.setData({
        itemOk4: true,
        xxitems4: oldItemxx4
      })

      var tData = that.data
      if (tData.itemOk1 && tData.itemOk2 && tData.itemOk3 && tData.itemOk4 && tData.itemOk5) {
        that.setData({
          unlockText: '开锁'
        })
      }
    } else if (okOld) {
      if (checkedName.indexOf("0") > -1 && checkedName.length == 1) {
        return
      }

      var oldItemxx4 = that.data.xxitems4
      for (var i in checkedName) {
        var t = checkedName[i]
        oldItemxx4[t - '0'].checked = true
      }
      oldItemxx4[0].checked = false
      this.setData({
        xxitems4: oldItemxx4,
        itemOk4: false,
        unlockText: '拒绝'
      })
    }
  },

  onCheckboxChange5: function (e) {
    var that = this
    var checkedName = e.detail.value
    console.log("e.detail: ", e)

    var len = checkedName.length
    var last = checkedName[len - 1]
    console.log('last  ', last)
    var newCheckedName = []
    if (0 == last) {
      checkedName = ["0"]
    } else {
      for (var i of checkedName) {
        if (i != '0') {
          newCheckedName.push(i)
        }
      }
      checkedName = newCheckedName
    }
    that.data.choices5 = checkedName

    this.setData({
      itemChecked5: (checkedName.length > 0)
    })

    var okOld = that.data.itemOk5
    if (checkedName.indexOf("0") > -1 && !okOld) {
      var oldItemxx5 = that.data.xxitems5
      oldItemxx5[0].checked = true
      for (var i = 1; i < oldItemxx5.length; ++i) {
        oldItemxx5[i].checked = false
      }
      that.setData({
        itemOk5: true,
        xxitems5: oldItemxx5
      })

      var tData = that.data
      if (tData.itemOk1 && tData.itemOk2 && tData.itemOk3 && tData.itemOk4 && tData.itemOk5) {
        that.setData({
          unlockText: '开锁'
        })
      }
    } else if (okOld) {
      if (checkedName.indexOf("0") > -1 && checkedName.length == 1) {
        return
      }

      var oldItemxx5 = that.data.xxitems5
      for (var i in checkedName) {
        var t = checkedName[i]
        oldItemxx5[t - '0'].checked = true
      }
      oldItemxx5[0].checked = false
      this.setData({
        xxitems5: oldItemxx5,
        itemOk5: false,
        unlockText: '拒绝'
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log('optionsss: ', options.data)

    // var listTmp = options.data.split(';')
    // var deviceName = listTmp[0]
    // wx.setStorageSync('curDevice', deviceName)
    // var phone = listTmp[1]
    // this.setData({
    //   device_name: deviceName,
    //   worker_phone: phone
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})